﻿@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<input style="display:none" id="ParentId" value="">
<blockquote class="layui-elem-quote layui-text">
    定义录入表步骤：1：新建录入表；2：添加指标并设置指标属性及数据规范；3：添加子表；4：创建数据表；5：定义录入校验及数据读写...；6：表单设计；7：代码生成
</blockquote>
<div class="layui-fluid">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-md4" style="padding-top: 15px; padding-right: 20px;">
            <fieldset class="table-fieldset">
                <legend><a id="btn_refresh">  <span class="layui-left-nav" style="color:blue"><i class='fa fa-refresh'>刷新</i></span></a> </legend>
                <div id="tbTree" style="overflow:auto;height:350px;"></div>
            </fieldset>
        </div>
        <div class="layui-col-md7" style="padding-top: 15px; padding-left: 20px;">
            <div class="layui-card">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn  layui-btn-normal" lay-event="add01" id="add01"><i class="fa fa-plus"></i>新建主表</button>
                        <button class="layui-btn layui-btn  layui-btn-normal" lay-event="add02" id="add02"><i class="fa fa-plus"></i>新建子表</button>
                        <button class="layui-btn layui-btn layui-btn-normal" lay-event="sort" id="sort"><i class="fa fa-paste"></i>分类管理</button>
                    </div>
                </div>
                <div id="divtb" style="display: none">
                    <div class="layui-form-item">
                        <label class="layui-form-label">编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="TbId" id="TbId" class="layui-input" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="TbName" id="TbName" class="layui-input" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-col-md3">
                        </div>
                        <div class="layui-col-md3">
                        </div>
                        <div class="layui-col-md3">
                        </div>
                        <div class="layui-col-md3">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm  layui-btn-primary" id="btn11"> <span style="color: #337ab7; font-weight: 700"><i class="fa fa-edit"></i>1.基本属性</span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm  layui-btn-primary" id="btn12"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-bars"></i>2.录入指标<span id="indexCount"></span></span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm  layui-btn-primary" id="btn13"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-database"></i>3.数据表</span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm  layui-btn-primary" id="btn14"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-table"></i>4.样式初始化</span></button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm  layui-btn-primary" id="btn21"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-folder-open-o"></i>5.左侧菜单</span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm layui-btn-primary" id="btn22"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-user"></i>6.权限设置</span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm  layui-btn-primary" id="btn23"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-edit"></i>页面按钮<span id="ButCount"></span></span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm   layui-btn-primary" id="btn24"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-paperclip"></i>公共附件设置</span></button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm  layui-btn-primary" id="btn31"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-exchange"></i>数据读写<span id="TbRelationCount"></span></span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm layui-btn-primary" id="btn32"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-check-square-o"></i>录入校验<span id="ValueCheckCount"></span></span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm layui-btn-primary" id="btn41"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-file-excel-o"></i>数据导入</span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm layui-btn-primary" id="btn42"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-file-text-o"></i>记录查看</span></button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm layui-btn-primary" id="btn99"><span style="color: #337ab7; font-weight: 700"><i class="fa fa-code"></i>查看代码</span></button>
                        </div>
                        <div class="layui-col-md3">
                            <button class="layui-btn layui-btn-sm  layui-btn-danger" id="btn43"><i class="fa fa-trash-o"></i>删除录入表</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['tree', 'layer', 'util', 'exLayer', 'exUtils', 'miniPage'], function () {
        let tree = layui.tree;
        let layer = layui.layer;
        let util = layui.util;
        let index = 100;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        let $ = layui.$;

        var openWH = miniPage.getOpenWidthHeight();

        tree.render({
            elem: '#tbTree',
            data: eval('(' + getData() + ')'),
            id: 'treeTb',
            click: function (obj) {
                var nodes = document.getElementsByClassName("layui-tree-txt");
                for (var i = 0; i < nodes.length; i++) {
                    if (nodes[i].innerHTML === obj.data.title) {
                        nodes[i].style.fontWeight = "bold";
                    } else {
                        nodes[i].style.fontWeight = "normal";
                    }
                }

                getTbInfo(obj.data.id);
            },
            showCheckbox: false,  //是否显示复选框
            accordion: 0,  //是否开启手风琴模式
            onlyIconControl: true, //是否仅允许节点左侧图标控制展开收缩
            isJump: 0,  //点击文案跳转地址
            edit: false  //操作节点图标
        });

        //彻底删除
        $("#btn43").click(function () {
            exLayer.confirm("将删除录入表,如果该录入表是主表将会删除该录入表包含的子表，录入表数据及为该录入表所做的配置信息将被删除，确定继续吗？", function () {
                var tbid = $("#TbId").val();
                exUtils.ajax("/SysTable/TbBasic/DeletTbAll", "post", { tbid: $("#TbId").val() }, true).done(function (data) {
                    tree.reload('treeTb', { data: eval('(' + getData() + ')') });
                    $("#TbId").val("");
                    $("#TbName").val("");
                    $("#ParentId").val("");
                    $("#divtb").hide();
                    exUtils.tableSuccessMsg('删除成功！');
                }).fail(function (error) {
                    console.log(error);
                });
                return false;
            })
        });

        //新增主表
        $("#add01").click(function () {
            exLayer.open("新建主表", "/SysTable/TbBasic/Add01", openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        });

        //新增子表
        $("#add02").click(function () {
            exLayer.open("新建子表", "/SysTable/TbBasic/Add02", openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        });

        //分类管理
        $("#sort").click(function () {
            exLayer.openMiddle("录入表分类", "/SysBasic/Sort/List?classid=CAT_table", '500px', '450px', null, null);
        });

        //基本属性
        $("#btn11").click(function () {
            var tbid = $("#TbId").val();
            if ($("#ParentId").val() == "") {
                exLayer.open($("#TbName").val(), "/SysTable/TbBasic/Edit01?tbid=" + tbid, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
            }
            else {
                exLayer.open($("#TbName").val(), "/SysTable/TbBasic/Edit02?tbid=" + tbid, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
            }
        });

        //录入指标
        $("#btn12").click(function () {
            var tbid = $("#TbId").val();
            if ($("#ParentId").val() == "") {
                exLayer.open($("#TbName").val() + "--录入指标管理", "/SysTable/TbSetUp/TbIndexList?tbid=" + tbid, '100%', '100%', '0px', '0px', null, null);
            }
            else {
                exLayer.open($("#TbName").val() + "--录入指标管理", "/SysTable/TbSetUp/ColumnList?tbid=" + tbid, '100%', '100%', '0px', '0px', null, null);
            }
        });

        //数据表
        $("#btn13").click(function () {
            var tbid = $("#TbId").val();
            exLayer.open($("#TbName").val() + "--数据表管理", "/SysTable/TbSetUp/DBTable?tbid=" + tbid, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        });

        //权限设置
        $("#btn22").click(function () {
            var tbid = $("#TbId").val();
            exLayer.open($("#TbName").val() + "--权限设置管理", "/SysTable/TbBasic/TbLimits?tbid=" + tbid, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        });

        //附件设置
        $("#btn24").click(function () {
            var tbid = $("#TbId").val();
            exLayer.open($("#TbName").val() + "--公共附件设置", "/SysTable/TbBasic/TbAtt?tbid=" + tbid, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        });

        //填报页面按钮
        $("#btn23").click(function () {
            var tbid = $("#TbId").val();
            exLayer.open($("#TbName").val() + "--编辑页面按钮设置", "/SysTable/TbBasic/TbBut?tbid=" + tbid, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        });

        //录入校验
        $("#btn32").click(function () {
            var tbid = $("#TbId").val();
            exLayer.open($("#TbName").val() + "--录入校验管理", "/SysTable/TbValueCheck/List?tbid=" + tbid, '100%', '100%', '0px', '0px', null, null);
        });

        //数据读写
        $("#btn31").click(function () {
            exLayer.open($("#TbName").val() + "--数据读写管理", "/SysTable/TbRelation/List?tbid=" + $("#TbId").val(), '100%', '100%', '0px', '0px', null, null);
        });

        //记录
        $("#btn42").click(function () {
            var tbid = $("#TbId").val();
            exLayer.open($("#TbName").val() + "--操作记录", "/SysTable/TbBasic/RecordList?tbid=" + tbid, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        });

        $("#btn99").click(function () {
            var tbid = $("#TbId").val();
            if ($("#ParentId").val() == "") {
                exLayer.open($("#TbName").val() + "--代码查看", "/SysTable/CodeBuild/LookCodeBuild?tbid=" + tbid, '100%', '100%', '0px', '0px', null, null);
            }
            else {
                layer.msg("请选择主表");
            }
        });

        //刷新
        $("#btn_refresh").click(function () {
            tree.reload('treeTb', { data: eval('(' + getData() + ')') });
            $("#TbId").val("");
            $("#TbName").val("");
            $("#divtb").hide();
        });

        function getData() {
            var data;
            $.ajax({
                url: "/SysTable/TableList/GetTableTreeSelect",
                type: "post",
                async: false,
                success: function (result) {
                    data = result.extra;
                }
            });
            return data;
        };

        function getTbInfo(tbid) {
            if (tbid != "") {
                $("#TbId").val("");
                $("#TbName").val("");
                $("#divtb").hide();

                $("#divtb").show();
                exUtils.ajax("/SysTable/TbBasic/GetModel", "post", { tbid: tbid }, true).done(function (data) {
                    var obj = JSON.parse(data.extra);

                    $("#indexCount").html(obj.indexCount);
                    $("#TbRelationCount").html(obj.TbRelationCount);
                    $("#ValueCheckCount").html(obj.ValueCheckCount);
                    $("#ButCount").html(obj.ButCount);

                    $("#TbId").val(obj.TbId);
                    $("#ParentId").val(obj.ParentId);
                    if (obj.ParentId != '') {
                        $("#TbName").val("{子表}" + obj.TbName);
                    }
                    else {

                        if (obj.BInfo == '1') {
                            $("#TbName").val("{主表 基础信息表}" + obj.TbName);
                        }
                        else {
                            if (obj.FlowType == '1') {
                                $("#TbName").val("{主表 固定流程}" + obj.TbName);
                            }
                            else {
                                $("#TbName").val("{主表 自由流程}" + obj.TbName);
                            }
                        }
                    }

                }).fail(function (error) {
                    console.log(error);
                });
            }
        };
    });
</script>